package com.dgut.dao;

import com.dgut.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface UserDao {
    @Select("select * from user")
    //    多表查询 一对多
    @Results(id = "aa",value = {
            @Result(property = "id",column = "id",id=true),
            @Result(property = "name",column = "name"),
            @Result(property = "password",column = "password"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "email",column = "email"),
            @Result(property = "customers",many = @Many(select = "com.dgut.dao.CustomerDao.findCustomerByUid",fetchType = FetchType.LAZY),column = "id")
    })
    public List<User> findAllUser();

    @Select({"select * from user where id = #{id}"})
    //    多表查询一对多
//    @Results({
//            @Result(property = "id",column = "id",id=true),
//            @Result(property = "name",column = "name"),
//            @Result(property = "password",column = "password"),
//            @Result(property = "customers",many = @Many(select = "com.dgut.dao.CustomerDao.findCustomerByUid",fetchType = FetchType.LAZY),column = "id")
//    })
//    引用id=“aa”的results
//    @ResultMap("aa")
    public User findUserById(int id);

    @Select("select * from user where name = #{name} and password = #{password}")
    public Integer findUser(User user);

    @Insert("insert into user(name,password) values(#{name},#{password})")
    public void insertUser(String name, String password);

    @Select("select * from user where name = #{name}")
    @ResultMap("aa")
    public User findUserByName(String name);

    @Update("update user set name=#{name},phone=#{phone},email=#{email} where id = #{id}")
    public void updateUser(User user);
}
